//
//  ChildInfo.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-15.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ChildInfo.h"
#import "UIAlertView+Kirinrin.h"
#import "UtilHelper.h"
/*
 authenticationCode = "";
 birthday = "2008-10-23";
 cid = f39c942b455f3b9e014568b76b080074;
 dbDatetime = 1397638865751;
 deviceId = 864390001150861;
 deviceSafeId = 186107;
 deviceSos = 18610788317;
 deviceWhiteList = "";
 id = f39c942b4569b70a014569c4a357002e;
 kindergartenId = f39c942b455f3b9e014568469383004f;
 name = "\U987e\U5b50\U4fca";
 "s_birthday" = "";
 sex = 1;
 state = 0;
 status = 0;
 uid = "";
 */
@implementation ChildInfo
-(instancetype)initWithDictionary:(NSDictionary *)data{
    self = [super init];
    if (self) {
        self.authenticationCode = data[@"authenticationCode"];
        self.birthday = data[@"birthday"];
        self.cid = data[@"cid"];
        self.id = data[@"id"];
        NSTimeInterval dbDatetime = [data[@"dbDatetime"] doubleValue];
        self.dbDatetime = [NSDate dateWithTimeIntervalSince1970:dbDatetime];
        
        self.deviceId = data[@"deviceId"];
        self.deviceSafeId = data[@"deviceSafeId"];
        self.deviceSos = data[@"deviceSos"];
        self.deviceWhiteList = data[@"deviceWhiteList"];
        self.kindergartenId = data[@"kindergartenId"];
        self.name = data[@"name"];
        self.sex = [data[@"sex"] isEqualToString:@"0"]?Female:Male;
        self.status = data[@"status"];
        self.state = data[@"state"];
        self.uid = data[@"uid"];
        
        if (!([data[@"photo"]isEqual:[NSNull null]]||[data[@"photo"] isEqualToString:@""])) {
            self.photoUrl = [NSURL URLWithString:data[@"photo"]];
        }
        
        
        self.option0 = 2;
        self.option1 = 2;
        self.option2 = 2;
        self.option3 = 2;
        
    }
    return self;
}

+(NSArray *)sortByGenderAndNameFromChildListFromeRemoteData:(NSArray *)dataList{
    NSMutableArray *tempList = [[NSMutableArray alloc]initWithArray:dataList];

    
    NSArray *array = [tempList sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        ChildInfo *child1 = (ChildInfo *)obj1;
        ChildInfo *child2 = (ChildInfo *)obj2;

        return [child1.getFirstName compare:child2.getFirstName];
    }];
    
    NSMutableArray *femaleArray = [[NSMutableArray alloc]init];
    NSMutableArray *maleArray = [[NSMutableArray alloc]init];
    for (ChildInfo *child in array) {
        if (child.sex == Female) {
            [femaleArray addObject:child];
        }else {
            [maleArray addObject:child];
        }
    }
    

    return @[femaleArray, maleArray];
}

+(NSArray *)getScoreDataFromChildArray:(NSArray *)childArray{
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    for (ChildInfo *child in childArray) {
        NSDictionary *data = @{@"childId":child.id, @"name":child.name, @"memo": (child.memo == nil)?[NSNull null]:child.memo, @"option0":@(child.option0), @"option1":@(child.option1), @"option2":@(child.option2), @"option3":@(child.option3)};
        NSLog(@"data.name = %@", data);
        [temp addObject:data];
    }
    
    return temp;
}

- (NSString *) getFirstName {
    if ([_name canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语
        return _name;
    }
    else { //如果是非英语
        return [NSString stringWithFormat:@"%c",[UtilHelper pinyinFirstLetter:[_name characterAtIndex:0]]];
    }
    
}

- (NSDictionary *)getScoreData{
    NSDictionary *result = @{@"childId":self.id, @"name":self.name, @"memo":self.memo, @"option0":@(self.option0), @"option1":@(self.option1), @"option2":@(self.option2), @"option3":@(self.option3)};
    
    return result;
}

- (void)setScoreOption:(NSUInteger)option0 :(NSUInteger)option1 :(NSUInteger)option2 :(NSUInteger)option3{
    self.option0 = option0;
    self.option1 = option1;
    self.option2 = option2;
    self.option3 = option3;
}
@end
